<?php


namespace app\index\controller;
use think\Controller;
use think\Db;

class Deletekf extends Controller
{




//    补费
    public function index()
    {
        $time='2020-8-4';  // 扣除的这天
        $delMoney=5000;   //需要删除的金额
        $total_money=0;   //已经删除的金额

        $rows=Db::table('tb_money')
            ->where(['keyid'=>['>',0]])
//            ->where(['keyid'=>14510])
            ->whereTime('intime', 'between', [$time.' 00:00:00', $time.' 23:59:59'])
            ->limit(10)
            ->order('intime ')
            ->select();
        if($rows){
            foreach ($rows as $k => $v) {
                $money=$v['money'];
                if ($total_money < $delMoney) {
                    $total_money += $money ;

//////          更新该词 消费金额，扣费状态更改
                    Db::table('tb_webkey')->where(['id' => $v['keyid']])->update(['moneypay'=>Db::raw('moneypay-'.$money)]);
//       更新用户余额，消费金额
                    Db::table('tb_user')->where('id', $v['userID'])->update([
                        'moneypay'=>Db::raw('moneypay-'.$money),
                        'money'=>Db::raw('money+'.$money)
                    ]);


                    Db::table('tb_money')->where('id',$v['id'])->delete();
                    Db::table('tb_admin')->where(['id' => $v['seoid']])->update(['M_money'=>Db::raw('M_money-'.$money)]);
//////       删除扣费记录
                    print($v['id'] . ':返还'.$money.'--------<br>');
                } else {
                    print($v['id'] . ':不扣费<br>');
                }
            }
            echo ("<script>setTimeout('window.location.reload()',700);</script>");

        }else{
            print($total_money.'<br>');
            print('完成');
        }





    }

}